package com.dnzx.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.dnzx.framework.page.PageResult;
import org.springframework.stereotype.Service;

import com.dnzx.dao.ICartDao;
import com.dnzx.model.CartBO;
import com.dnzx.model.ParamBO;
import com.dnzx.model.SubCategoryBO;
import com.dnzx.service.ICartService;
import com.dnzx.service.ICategoryService;
import com.dnzx.service.IParamService;
import com.dnzx.service.ISubCategoryService;

/**
 * 订单
 */
@Service
public class CartServiceImpl implements ICartService {

    @Resource
    private ICartDao cartDao;
    
    @Resource
    private ICategoryService categoryService;
    @Resource
    private IParamService paramService;
    @Resource
    private ISubCategoryService subCategoryService;

    @Override
    public void save(CartBO cartBO) throws Exception {
    	double amt = 0;
    	List<ParamBO> paramBOList = this.paramService.queryList(new ParamBO());
    	Map<String, ParamBO> paramBOMap = paramBOList.stream().collect(Collectors.toMap(ParamBO::getCode, a -> a,(k1,k2)->k1));
    	
    	// 主柜价格
    	Long categoryId = cartBO.getCategoryId();
    	amt = amt + this.categoryService.get(categoryId).getPrice();
    	// 操作系统价格
    	amt = amt + Double.valueOf(paramBOMap.get(cartBO.getOperationCode() + "_PRICE").getParamValue());
    	// 处理设备收款方式
    	List<String> paymentTypeDeviceList = cartBO.getPaymentTypeDeviceList();
    	if (paymentTypeDeviceList != null && paymentTypeDeviceList.size() > 0) {
    		String paymentTypeDevice = "";
    		for (String paymentTypeDeviceTemp : paymentTypeDeviceList) {
    			paymentTypeDevice = paymentTypeDevice + "," + paymentTypeDeviceTemp;
    			// 设备收款方式价格
    			amt = amt + Double.valueOf(paramBOMap.get("DEVICE_" + paymentTypeDeviceTemp + "_PRICE").getParamValue());
    		}
    		paymentTypeDevice = paymentTypeDevice.substring(1, paymentTypeDevice.length());
    		cartBO.setPaymentTypeDevice(paymentTypeDevice);
    	}
    	// 需要服务器价格
    	if ("1".equals(cartBO.getServerFlag())) {
    		amt = amt + Double.valueOf(paramBOMap.get("SERVER_PRICE").getParamValue());
    	}
    	// 处理副柜编号和数量
    	List<String> subCategoryCodeList = cartBO.getSubCategoryCodeList();
    	List<String> subCategoryCountList = cartBO.getSubCategoryCountList();
    	if (subCategoryCodeList != null && subCategoryCodeList.size() > 0) {
    		String subCategoryCode = "";
    		String subCategoryCount = "";
    		for (int i = 0; i < subCategoryCodeList.size(); i++) {
    			String subCategoryCodeTemp = subCategoryCodeList.get(i);
    			subCategoryCode = subCategoryCode + "," + subCategoryCodeTemp;
    			
    			String subCategoryCountTemp = subCategoryCountList.get(i);
    			subCategoryCount = subCategoryCount + "," + subCategoryCountTemp;
    			
    			// 计算副柜价格
    			SubCategoryBO subCategoryBOQuery = new SubCategoryBO();
    			subCategoryBOQuery.setCode(subCategoryCodeTemp);
    			subCategoryBOQuery.setCategoryId(cartBO.getCategoryId());
    			SubCategoryBO subCategoryBO = this.subCategoryService.get(subCategoryBOQuery);
    			amt = amt + subCategoryBO.getPrice() * Integer.valueOf(subCategoryCountTemp);
    		}
    		// 门板颜色定制价格
    		if (!"".equals(cartBO.getColorValue())) {
    			amt = amt + Double.valueOf(paramBOMap.get("OTHER_COLOR_PRICE").getParamValue());
    		}
    		// 柜体颜色定制价格
    		if (!"".equals(cartBO.getCabinetColorValue())) {
    			amt = amt + Double.valueOf(paramBOMap.get("CABINET_COLOR_PRICE").getParamValue());
    		}
    		// 需要顶棚价格
    		if ("1".equals(cartBO.getCeilingFlag())) {
    			amt = amt + Double.valueOf(paramBOMap.get("CEILING_PRICE").getParamValue());
    		}
    		// 需要底脚价格
    		if ("1".equals(cartBO.getFootingFlag())) {
    			amt = amt + Double.valueOf(paramBOMap.get("FOOTING_PRICE").getParamValue());
    		}
    		// 需要木箱包装价格
    		if ("1".equals(cartBO.getWoodFlag())) {
    			amt = amt + Double.valueOf(paramBOMap.get("WOOD_PRICE").getParamValue());
    		}
    		// 需要摄像头价格
    		if ("1".equals(cartBO.getCameraFlag())) {
    			amt = amt + Double.valueOf(paramBOMap.get("CAMERA_PRICE").getParamValue());
    		}
    		
    		subCategoryCode = subCategoryCode.substring(1, subCategoryCode.length());
    		subCategoryCount = subCategoryCount.substring(1, subCategoryCount.length());
    		cartBO.setSubCategoryCount(subCategoryCount);
    		cartBO.setSubCategoryCode(subCategoryCode);
    	}
    	String sn = "CART_" + new Date().getTime();
    	cartBO.setSn(sn);
    	cartBO.setAmt(amt);
        this.cartDao.save(cartBO);
    }

    @Override
    public void delete(Long id) throws Exception {
        this.cartDao.delete(id);
    }

    @Override
    public void update(CartBO cartBO) throws Exception {
        this.cartDao.update(cartBO);
    }

    @Override
    public CartBO get(Long id) {
        return this.cartDao.get(id);
    }
    
    @Override
    public CartBO get(CartBO cartBO) {
    	List<CartBO> cartBOList = this.cartDao.queryList(cartBO);
    	if (cartBOList != null && cartBOList.size() > 0) {
    		return cartBOList.get(0);
    	}
        return null;
    }
    
    @Override
    public List<CartBO> queryList(CartBO cartBO) {
        return this.cartDao.queryList(cartBO);
    }
    
    @Override
    public PageResult<List<CartBO>> queryPage(CartBO cartBO, PageResult<List<CartBO>> pageResult) {
        int totalRows = this.cartDao.queryCount(cartBO);
        pageResult.setTotalRows(totalRows);
        pageResult.setCurrentPage(cartBO.getCurrentPage());
        pageResult.setPageSize(cartBO.getPageSize());
		List<CartBO> records = this.cartDao.queryPage(pageResult.getPageSize(), pageResult.getStartRow(), cartBO);
		if (records != null && records.size() > 0) {
			for (CartBO cartBOTemp : records) {
				String subCategoryCode = cartBOTemp.getSubCategoryCode();
				String subCategoryCount = cartBOTemp.getSubCategoryCount();
				if (subCategoryCode != null && !"".equals(subCategoryCode)) {
					String[] subCategoryCodes = subCategoryCode.split(",");
					String[] subCategoryCounts = subCategoryCount.split(",");
					
					List<Map<String, String>> subCategoryMapList = new ArrayList<Map<String, String>>();
					for (int i = 0; i < subCategoryCodes.length; i++) {
						Map<String, String> subCategoryMap = new HashMap<String, String>();
						subCategoryMap.put("name", subCategoryCodes[i]);
						subCategoryMap.put("count", subCategoryCounts[i]);
						subCategoryMapList.add(subCategoryMap);
					}
					cartBOTemp.setSubCategoryMapList(subCategoryMapList);
				}
			}
		}
		pageResult.setT(records);
		return pageResult;
    }

}
